home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1990: Discy Business / Discy Business.2mg / DEV.CD / TOOLS / TECH.NOTES / IIGS / TN.IIGS.081 < prev    next >
Encoding:
Text File  |  1990-04-29  |  16.2 KB  |  329 lines  |  [04] ASCII Text (0x0000)

  1. Apple II
  2. Technical Notes
  3. _____________________________________________________________________________
  4.                                                   Developer Technical Support
  5.  
  6. Apple IIgs
  7. #81:    Extended Control Ecstasy
  8.  
  9. Written by:    C.K. Haun                                             May 1990
  10.  
  11. This Technical Note discusses special features and concerns that should be 
  12. considered when using the extended controls introduced in System Software 5.0.
  13. _____________________________________________________________________________
  14.  
  15. Introduction
  16.  
  17. The extended controls introduced in System Software 5.0 allow the application 
  18. programmer a great deal more freedom in designing and controlling 
  19. applications.  The new features enhance the functionality of the controls and 
  20. TaskMaster, but can cause confusion and consternation if you are careless with 
  21. the new parameter block .  This Note also includes a discussion of the 
  22. multipart nature of many of the extended controls and some pointers for 
  23. writing extended custom controls.
  24.  
  25. Counting The Costs
  26.  
  27. One of the major stumbling blocks seen when programming the new extended 
  28. controls is bad parameter counts.  Extended controls introduce parameter 
  29. blocks and parameter counts to the Control Manager.  You need to fully 
  30. understand the parameters required and the resulting parameter count for each 
  31. control you create, or you can experience program problems that may be very 
  32. confusing and difficult to track down.
  33.  
  34. Remember also that the Control Manager does not understand "skipping 
  35. parameters."  If you are creating an extended radio button and you want key 
  36. equivalents, but not a color table, you cannot ignore the color table 
  37. parameter field.  There is no way to tell the Control Manager to "skip" a 
  38. field during control creation; make sure you initialize all the fields to 
  39. values that are meaningful--either a real pointer, handle, resource ID, or 
  40. zeroes.  In this case, if you try to "skip" the color table and do not zero 
  41. out the color table parameter field, the resulting radio button wears ugly 
  42. colors you do not expect.
  43.  
  44. As you can imagine, miscounting parameters in other extended controls can 
  45. produce confusing results, so the parameter count is the first place you 
  46. should check when you're having difficulties creating extended controls.
  47.  
  48. For Rez users, the Types.Rez file contains extended control templates that 
  49. automatically generate the correct count value, so programmers creating all 
  50. their controls with the Resource Compiler should not have these problems.
  51.  
  52. Silly Little Bits
  53.  
  54. The other area of the new parameter block model that is giving folks trouble 
  55. is the moreflags field.  This field hones the definition given by the 
  56. reference fields and can cause you much grief if misused.  Make sure to set 
  57. the reference bits to the values you require.  The bit settings have been 
  58. standardized across all the extended controls, with %00 indicating a pointer, 
  59. %01 indicating a handle, and %10 indicating a resource.  Remember also to set 
  60. the bits for all the references you have--strings, color tables, or whatever 
  61. else may be ambiguously referenced in the control.
  62.  
  63. If you accidentally use the wrong bit pattern, you can experience strange bugs 
  64. ranging from garbled text to SysFailMgr caused by a nonexistent resource being 
  65. referenced.  Again, Rez users can use the equates for all the reference 
  66. specifiers in the Types.Rez file to avoid confusion and evil bugs.
  67.  
  68. The Parts are Greater than the Whole
  69.  
  70. To create some new extended controls, like pop-up menu controls and LineEdit 
  71. controls, functions of different tool sets were combined.  Pop-up menu 
  72. controls are a combination of the Control Manager and the Menu Manager, 
  73. LineEdit controls are a blending of the Control Manager and the LineEdit tool 
  74. set, and other new control types follow the same pattern.
  75.  
  76. This means that, at times, you have to go further into the documentation to 
  77. find information.  Getting the text out of an LineEdit extended control is a 
  78. multi-step process that is a good example of this type of problem.
  79.  
  80. MyLineEdit dc     i2'8'                   ; parameter count
  81.            dc     i4'1'                   ; id number 1
  82.            dc     i2'10,10,23,90'         ; control rectangle
  83.            dc     i4'editLineControl'     ; process reference
  84.            dc     i2'0'                   ; flags
  85.            dc     i2'fCtlCanBeTarget+fCtlWantEvents+fCtlProcRefNotPtr'
  86.                                           ; moreflags
  87.            dc     i2'0'                   ; refcon
  88.            dc     i2'15'                  ; maximum characters allowed
  89.            dc     i4'0'                   ; no default text
  90. MyLineEditHandle ds 4                     ; handle for the created control
  91.  
  92.            pea    0
  93.            pea    0
  94.            pea    0                       ;verb, single Extended control
  95.            pushlong #MyLineEdit
  96.            pushlong mywindowgrafport      ; window that control will reside in
  97.            _NewControl2
  98.            pla
  99.            sta    MyLineEditHandle        ; save the control handle
  100.            pla
  101.            sta    MyLineEditHandle+2
  102.  
  103.  
  104. When you want to get the text back out of that control later, you begin to 
  105. experience what it means to have a control that is an amalgam of various 
  106. tools.  You would start by using the control handle returned by NewControl2:
  107.  
  108. Scratch    equ    $0                      ; some scratch space
  109. Scratch2   equ    $4
  110.  
  111.            lda    MyLineEditHandle        ; move the ControlHandle to
  112.            sta    Scratch                 ; some direct page space
  113.            lda    MyLineEditHandle+2
  114.            sta    Scratch+2
  115.            lda    [Scratch]
  116.            tax
  117.            ldy    #2
  118.            lda    [Scratch],y             ; and dereference it, putting it 
  119.            sta    Scratch+2               ; back in some dpage space to 
  120.            stx    Scratch                 ; use it.
  121.  
  122. That gives you the pointer to the control record.  Stored in the control 
  123. record is the handle of the LineEdit item that is actually controlling the 
  124. text processing:
  125.  
  126.            pea    0                       ; make space for the text handle to 
  127.            pea    0                       ; be returned
  128.            ldy    #octlData               ; offset to the ctlData section
  129.                                           ; of the ControlRecord
  130.            lda    [Scratch],y             ; where the handle for the actual 
  131.            tax                            ; LineEdit item was stored
  132.            iny
  133.            iny
  134.            lda    [Scratch],y
  135.            pha
  136.            phx
  137.            _LEGetTextHand                 ; ask for the handle for the text 
  138.            pla                            ; in this LineEdit control
  139.            sta    Scratch2                ; and now you have the handle to the 
  140.            pla                            ; text you want.
  141.            sta    Scratch2+2
  142.  
  143. The main point is that when you are using extended controls, you often cannot 
  144. use the Control Manager to do everything that needs to be done.  You also need 
  145. to understand and use the supplementary or "hidden" tool sets.
  146.  
  147. Here's another example, using a pop-up menu extended control, and in this case 
  148. we define a font pop-up that contains all the font names currently available.
  149.  
  150. MyPopUpControl dc i2'9'                   ; parameter count of 9 
  151.            dc    i4'1'                    ; control ID of 1
  152.            dc    i2'2,2,0,0'              ; Position, upper left corner of the 
  153.                                           ; window, let Control Manager 
  154.                                           ; calculate full size
  155.            dc    i4'popUpControl'         ; def proc for PopUp
  156.            dc    i2'0'                    ; flags
  157.            dc    i2'fCtlWantEvents+fCtlProcRefNotPtr'
  158.                                           ; more flags
  159.            dc    i4'0'                    ; ref con
  160.            dc    i2'0'                    ; title width, will be calculated
  161.            dc    i4'mymenu'               ; pointer to actual menu structure
  162.            dc    i2'500'                  ; initial value, item number of item
  163.                                           ; to be displayed in popup at 
  164.                                           ; creation
  165.  
  166.  
  167. mymenu     dc    i2'0'                    ; version number, should be 0
  168.            dc    i2'200'                  ; menu ID number
  169.            dc    i2'0'                    ; menu flags
  170.            dc    i4'mymenutitle'          ; pointer to menu title
  171.            dc    i4'mymenuitem1'          ; first menu item
  172.            dc    i4'mymenuitem2'          ; second menu item
  173.            dc    i4'0'                    ; null terminator, end of menu
  174. mymenutitle str 'Font'
  175.  
  176. mymenuitem1 dc i2'0'                      ; version number
  177.            dc i2'500'                     ; item number
  178.            dc i2'0'                       ; no hot keys
  179.            dc i2'0'                       ; not checked
  180.            dc i2'0'                       ; item flags, no special drawing
  181.            dc i4'mymenuitem1title'
  182. mymenuitem1title str 'Plain'
  183.  
  184. mymenuitem2 dc i2'0'                      ; version number
  185.            dc i2'501'                     ; item number
  186.            dc i2'0'                       ; no hot keys
  187.            dc i2'0'                       ; not checked
  188.            dc i2'1'                       ; item flags, bold face this one
  189.            dc i4'mymenuitem2title'
  190. mymenuitem2title str 'Bold'
  191.  
  192. Now create this control:
  193.  
  194.            dc     i2'501'                 ; item number
  195.            pea    0
  196.            pea    0
  197.            pushlong mywindow              ; target window grafptr
  198.            pea    0                       ; verb, single control pointer
  199.            pushlong #MyPopUpMenu
  200.            _NewControl2
  201.            pulllong mypopuphandle         ; save the handle
  202.  
  203. This pop-up menu control created is not associated with the menu bar across 
  204. the top of the desktop.  You can consider each of your pop-up menu controls as 
  205. separate menu bars, so if you want to perform Menu Manager calls on a pop-up 
  206. menu control, you need to set the menu to point at your pop-up menu control.  
  207. In this example, to add all the fonts available to the pop-up menu you would:
  208.  
  209.            pea    0
  210.            pea    0                       ; space to hold current bar
  211.            _GetMenuBar                    ; get the handle to the current 
  212.                                           ; menu bar
  213.            pushlong mypopuphandle
  214.            _SetMenuBar
  215.            pea    200                     ; id number of this menu
  216.            pea    502                     ; first font family ID number to use
  217.            pea    0                       ; fontspecbits
  218.            _FixFontMenu
  219.            pea    0
  220.            pea    0
  221.            pea    200
  222.            _CalcMenuSize                  ; re-size the popup menu
  223.            _SetMenuBar                    ; restore the previous menu as the 
  224.                                           ; current menu
  225.  
  226. Controls That Are Not Controls
  227.  
  228. The new picture extended control is not a "full-fledged" control; it has been 
  229. provided to simplify your programming tasks.  The picture control does not 
  230. support normal mouse hit testing and highlighting.  Think of it as a built-in 
  231. extension to your content drawing routine, and not as a control.  It is 
  232. provided to allow you to refresh your whole window with a single DrawControls 
  233. call, instead of drawing the controls and then drawing pictures.  The icon 
  234. button extended control has been provided as the graphic full-function 
  235. control.  If you need or want a fully functional control that uses a picture, 
  236. you should consider writing your own custom control procedure.
  237.  
  238. It's Not Dirty, It's Text
  239.  
  240. There has been some confusion about determining if a TextEdit record has been 
  241. changed.  The documentation has been a little vague, and the process itself 
  242. has mislead some people.  Here is The Truth:  there is a TextEdit dirty flag, 
  243. and you can use it and rely on it to tell you when a TextEdit record has 
  244. changed.
  245.  
  246. The TextEdit dirty flag is bit 6 (fRecordDirty in the E16.TextEdit interface 
  247. file) of the ctlFlag byte.  This has caused some confusion because the ctlFlag 
  248. byte is at offset $12 in the control definition template, and it is at offset 
  249. $10 in the TextEdit or Control record.  Just remember that it is not in the 
  250. same place in the record as it is in the template.
  251.  
  252. If it is set, then the TextEdit or Control record has been changed since the 
  253. last time the dirty bit was cleared.  The dirty bit is clear initially when 
  254. you create the TextEdit or Control record.  Anytime after that, if the user 
  255. enters text into the TextEdit record, TextEdit sets the dirty flag.  It is up 
  256. to your application to clear the dirty flag; TextEdit has no way of knowing 
  257. when you've saved or cleared data.
  258.  
  259. Custom Extended Controls
  260.  
  261. Custom controls can also benefit from all the advantages of extended controls.  
  262. You can create a custom control that uses a template, can be a resource, has a 
  263. definition procedure that is a resource, and responds to all the new control 
  264. calls.  If you write an extended custom control or upgrade a previously-
  265. written custom control, there are new messages and changes to existing 
  266. messages of which you need to be aware.  These changes are documented in 
  267. volume 3 of the Apple IIgs Toolbox Reference. 
  268.  
  269. Putting your custom control definition procedure in a resource can 
  270. significantly enhance the functionality of the custom control.  You may find 
  271. it easier to add to all of your programs and you do not have to manage the 
  272. code space required. If you do write a custom control definition procedure and 
  273. want to store it as a resource, here are some hints for success.
  274.  
  275. First, the code you store in your resource fork must be fully compiled and 
  276. linked code.  The code resource converter uses the System Loader to load the 
  277. code, so the code must be executable code, not object code.
  278.  
  279. Second, be sure to set the fixed and convert bits of the resource attributes 
  280. for your code resource.  The fixed bit needs to be set so your code does not 
  281. move once it's been loaded and relocated.  The convert bit must be set to tell 
  282. the Resource Manager to call the code resource converter when it loads this 
  283. resource. The resource type for control definition procedures is rCtlDefProc, 
  284. $800C.
  285.  
  286. Third, keep in mind that this definition procedure may be purged and reloaded 
  287. whenever the Memory Manager needs the space.  This means that you cannot store 
  288. any information in your definition procedure if you want to keep track of it 
  289. between calls to the definition procedure.  If you do, and your definition 
  290. procedure gets purged and reloaded, you lose that data.
  291.  
  292. If you need data space for your custom control, use the control record as your 
  293. stash.  You can easily either use the fields already provided in the control 
  294. record, or you can expand the control record to as much space as you need 
  295. (within sensible limits) and store your data there.
  296.  
  297. Warning:  Control definition procedures are initially loaded with 
  298.           purge level zero.  When they are released, they are given 
  299.           purge level three.  If they are then reloaded, the Resource 
  300.           Manager does not change the purge level back to zero--your 
  301.           definition procedure may then be purged (even while 
  302.           executing) unless its handle is locked.  The solution is to 
  303.           lock your definition procedure handle within the procedure:
  304.  
  305.                    myPosition    pea    0              ; space for result
  306.                                  pea    0
  307.                                  pushLong #myPosition
  308.                                  _FindHandle
  309.                                  _HLock
  310.  
  311.           and unlock your handle with HUnlock on exit.  This keeps 
  312.           your procedure safe, while not creating "code islands," 
  313.           which clog up memory.
  314.  
  315. Conclusion
  316.  
  317. The extended controls provided in System Software 5.0 and later are a great 
  318. leap forward for programmers.  They relieve the application of much of the 
  319. tedious detail code that relates to housekeeping, not the guts of application 
  320. programming.  Used in combination with the enhanced TaskMaster, you can have 
  321. an application's visual interface up and running a lot faster, leaving you 
  322. more time to work on the heart of your application.
  323.  
  324.  
  325. Further Reference
  326. _____________________________________________________________________________
  327.   o  Apple IIgs Toolbox Reference, Volumes 1 through 3.
  328.  
  329.